﻿using System.Runtime.InteropServices;
using System.Text;

namespace Util.Ini {
    public class IniFile {
        public string _path;

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        private static extern uint GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public IniFile(string IniPath) { _path = IniPath; }

        public void SetValue(string Section, string Key, string Value) {
            WritePrivateProfileString(Section, Key, Value, _path);
        }
        public string GetValue(string Section, string Key, string DefaultValue = "") {
            StringBuilder temp = new StringBuilder(255);
            uint i = GetPrivateProfileString(Section, Key, DefaultValue, temp, 255, _path);
            return temp.ToString();
        }
        public string GetSection(string Section, string Key, string DefaultValue = "") {
            StringBuilder temp = new StringBuilder(255);
            uint i = GetPrivateProfileString(Section, Key, DefaultValue, temp, 255, _path);
            return temp.ToString();
        }
    }
}
